Showing posts with label VBA Code Snippet. Show all posts
Showing posts with label VBA Code Snippet. Show all posts

Friday, March 14, 2014

How to switch from main view to Endnote view

Leave a Comment


As I mentioned in the previous article, to make vba code work with endnote or footnote we need to switch the view to endnote or footnote view. In this article I show the code for switching view of word document.

How to switch the view to endnote view
First I use the function EndnotesExist to check whether there is endnote exists in the word document and then use ActiveWindow.ActivePane.View.Type to switch the view


≫ Detect Endnote
FunctionEndnotesExist() As Boolean
    DimStoryRange As Range
    ForEach StoryRange In ActiveDocument.StoryRanges
        IfStoryRange.StoryType = wdEndnotesStory Then
            EndnotesExist = True
            ExitFor
        End If
    Next StoryRange
EndFunction


Read More...

How to detect footnote and endnote whether they exist in word document

Leave a Comment


In this article, it is a bit strange that why we need to check whether the footnote or endnote exists or not with word document. The reason is that word document separates the document into these views, main document view and footnote or endnote view. When you run your macro code, it work only in the main view, so in the footnote or endnote views it doesn't work.

How to make macro work in footnote or endnote view
We need to detect the footnote or endnote and switch from main view to footnote or endnote view with ActiveWindow.ActivePane.View.Type.


Detect Endnote
FunctionEndnotesExist() As Boolean
    DimmyStoryRange As Range
    ForEach myStoryRange In ActiveDocument.StoryRanges
        IfmyStoryRange.StoryType = wdEndnotesStory Then
            EndnotesExist = True
            ExitFor
        End If
    Next myStoryRange
EndFunction

Read More...

How to convert footnote to endnote in word document

Leave a Comment


In this section, I discuss about footnote and endnote within word document and how convert from footnote to endnote

What is footnote?
A term used to describe additional information found at the bottom of a page call footnote

What is endnote?
Endnote is additional information or credits given at the end of the document instead of at the end of each page


How to convert from footnote to endnote
ActiveDocument.Footnotes.Convert

Read More...

How to remove manual line break using word vba

Leave a Comment


This part of code is for removing the all Manual Line Break in word document at once.

What is Manual line break?
A manual line break ends the current line and continues the text on the next line. For example, suppose your paragraph style includes extra space before each paragraph.

To get Manual Line Break
1. Click where you want to break a line of text.
2. Press SHIFT+ENTER.

How to remove
To remove Manual we need to search the for manual line break and replace it with space or nothing
Find Manual Line Break
Selection.Find.Text = "^l"

Replace with space
Selection.Find.Replacement.Text =  ""

Read More...

How to Delete empty paragraph and format within word document with word vba

Leave a Comment


Suppose that you want remove unwanted empty paragraphs within your word document, the following code snippet will show how that you can remove those empty paragraph.

What is empty paragraph?
It is a blank line that exists in the word document.

How to delete
In order to delete the empty paragraph from your word document, iterate all the paragraph with your document with ActiveDocument.Paragraphs and Check word count between 1 and 0.

Read More...

How increase, Decrease indent using word vba

Leave a Comment


Mostly to increase or decrease the indentation in word document, we use button increase or decrease indent in Home Tabs. In this article, I will show how to use the vba code to increase or decrease indentation. In this article, it include 6 types of the indentation, increase left indent, increase right indent, decrease left indent, decrease right indent, increase first line indent, and decrease first line indent. The following code show you the 6 types if indentation using vba by 0.5 cm.

Increase Left Indent
PublicSub IncreaseIndent()
    DimsDefaultIndent As Single
    DimsCalcul As Single
    Dimopara As Paragraph
   
    sDefaultIndent = 0.5
    sDefaultIndent = 0.5
    WithSelection
        Set opara = .Paragraphs.First
        sCalcul = opara.LeftIndent + CentimetersToPoints(sDefaultIndent)
        opara.LeftIndent = sCalcul
    EndWith
   
    Set opara = Nothing
EndSub

Read More...

How to detect font font size and change font size using word vba

Leave a Comment


In this article, I will discuss on how to detect the font size and change the font size to the size as we want to change with two examples of code. In the first example, it is about changing the font size within the defined range of font size and in the second one; it is about changing all font size to one defined size.
To work with the font size with vba, we need to use Word.Range to detect the font size and change the font size. Here are the two example of codes


Example 1: Changing font size withthe defined font size

SubStandardizeFontSize (oWord As Word.Range)
    IfoWord.Font.Size < 16 And oWord.Font.Size > 12 Then
        oWord.Font.Size = 16
    ElseIfoWord.Font.Size < 12 And oWord.Font.Size > 11 Then
        oWord.Font.Size = 12
    ElseIfoWord.Font.Size <= 11 And oWord.Font.Size > 9 Then
        oWord.Font.Size = 11
    ElseIfoWord.Font.Size <= 9 Then
        oWord.Font.Size = 10
    ElseIfoWord.Font.Size = 9999999 Then'999999 - represents word with characters of different size
        sFontSize = oWord.Characters(1).Font.Size
        Forj = 1 To oWord.Characters.Count
            IfoWord.Characters(j).Font.Size = sFontSize Then: oWord.Characters(j).Font.Size = 12
            Else: oWord.Characters(j).Font.Size = 10
            End If
        Next
    End If
EndSub

Read More...

How to Connect Paragraph using word vba

Leave a Comment


As I mentioned in the previous article, VBA allow us to automate your tasks, and your will be done quickly with vba because vba programming can perform many procedure at the same time. In this article, I will show how to connect two or more paragraphs together by just select the paragraph and then run the macro. Normally, when we want to connect to paragraph together, we need to delete the paragraph mark.
Suppose that we want to connect 20 paragraphs in word document, we need to delete the paragraph mark 20 times. However with vba programming, we can delete the 20 paragraphs marks at once.

How to delete the Paragraph Marks
Word VBA provides us the Selection object to word to selection text, paragraph, etc.

In order to delete paragraph mark using selection, first we need to select the paragraph using selection object
Select 5 paragraph from the position of cursor down


Selection.MoveDown Unit:=wdParagraph, Count:=5, Extend:=wdExtend

detect the paragraph and delete

Read More...

How to Change Font Format using VBA

Leave a Comment
In this section, I will show you on how to use vba code to change the format font.
In the following code, I discuss about the how to change the font format bold and italic while in th word document do not have the format of bold and Italic but it has the property of font name with bold or italic for instance Font Time New Roman-Bold. Firstly, in order to change the font format with this case, I need to detach the font name of each words or characters within the word document. And then create the function for finding and replacing with font format as we want to replace.

Detecting the font Propertry

To detect the font name properties in word document, I use vba code called: Range object  and ActiveDocument.content.Paragraphs to find the fount properties in each word or characters.
ActiveDocument.Content.Paragraphs

And then I compare the text string in each words
InStr(1, CStr(fontList(k)), "ITALIC", vbTextCompare)
InStr(1, CStr(fontList(k)), "BOLD", vbTextCompare)


Replacing font Format
In order to change the font bold or italic, I use the following function which take a paramer called font name and use this font for replacing styles

Public SubChangeFontFormatItalic(fontname As String)
    Selection.HomeKey
    Selection.WholeStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Font.Name = fontname
    Selection.Find.Replacement.Font.Italic = True
    With Selection.Find
        .Text = "^?"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

End Sub

'Change Font Name with Bold to Font Time New Roamn with Bold format
Public SubChangeFontFormatBold(fontname As String)
    Selection.HomeKey
    Selection.WholeStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Font.Name = fontname
    Selection.Find.Replacement.Font.Bold = True
    With Selection.Find
        .Text = "^?"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Full Code
Here is the full code of this tutorial section

Public Sub ChangeFormatFont
    Dim sTmp As String
    Dim i, j, k As Integer
    Dim opara As Paragraph
    Dim fontFont As Boolean
    Dim fontCount As Integer
    Dim fontList(200) As String
    Dim fontname As String
    Dim fontItalic As String
    Dim fontBold As String

    fontCount = 0
    For Each opara In ActiveDocument.Content.Paragraphs
        If opara.Range.Words.Count > 1 Then
            For i = 1 To opara.Range.Words.Count
                With opara.Range.Words(i)
                    fontname = .Font.Name
                    FoundFont = False
                    For k = 1 To fontCount
                       If fontList(k) = fontname Then FoundFont = True
                    Next k
                    If Not FoundFont Then
                        fontCount = fontCount + 1
                        fontList(fontCount) = fontname
                    End If
'
               ' Debug.Print .Font.Name
              
                End With
            Next i
        End If
    Next opara
    For k = 1 To fontCount
        If InStr(1, CStr(fontList(k)), "ITALIC", vbTextCompare) Then
            fontItalic = fontList(k)
            ChangeFontFormatItalic (fontItalic)
        End If
        If InStr(1, CStr(fontList(k)), "BOLD", vbTextCompare) Then
            fontBold = fontList(k)
            ChangeFontFormatBold (fontBold)
        End If
    Next k
    Debug.Print fontItalic
    Debug.Print fontBold
   
    Set opara = Nothing
    'MsgBox "Complete !!!"
End Sub

Public Sub ChangeFontFormatItalic(fontname As String)
    Selection.HomeKey
    Selection.WholeStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Font.Name = fontname
    Selection.Find.Replacement.Font.Italic = True
    With Selection.Find
        .Text = "^?"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

End Sub

'Change Font Name with Bold to Font Time New Roamn with Bold format
Public Sub ChangeFontFormatBold(fontname As String)
    Selection.HomeKey
    Selection.WholeStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Font.Name = fontname
    Selection.Find.Replacement.Font.Bold = True
    With Selection.Find
        .Text = "^?"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub


That is all for this tutorial. 
Read More...
DMCA.com Protection Status